package action;

import entity.Dept;
import entity.EmpVo;
import service.Service;
import service.impl.ServiceImpl;

import java.util.List;
import java.util.Scanner;

public class Action {
    Service S = new ServiceImpl();
    Scanner input = new Scanner(System.in);

    /**
     * 菜单页
     */
    public void menu() {
        System.out.println("欢迎使用知识管理系统");
        loginByAccount();
    }

    /**
     * 登录方法
     */
    public void loginByAccount() {
        System.out.println("请输入账户：");
        String account = input.next();
        EmpVo empVo = S.queryEmpByAccoubt(account);
        if (empVo != null) {
            System.out.println("当前账号为：" + empVo.getAccount());
            System.out.println("请输入密码：");
            String password = input.next();
            System.out.println("正在登录，请稍后...");
            if (password.equals(empVo.getPassword())) {
                if (empVo.getStatus() == 1) {
                    System.out.println("账号已停用");
                    logout();
                } else {
                    if (empVo.getDid() == 1) {
                        System.out.println("部门未分配，请联系人事部处理");
                    } else {
                        System.out.println("登录成功，正在进入系统>>>");
                        System.out.println("操作人：" + empVo.getLastname());
                        System.out.println(empVo.getDid());
                        in01(empVo.getDid(), empVo.getId());
                    }
                }
            } else {
                System.out.println("密码错误，请核对密码！！！");
                menu();
            }
        } else {
            System.out.println("账号不存在，请确认账号信息！！！");
            menu();
        }
    }

    /**
     * 退出系统
     */
    public void logout() {
        System.exit(0);
    }

    /**
     * 查部门列表
     *
     * @param did
     */
    public void in01(int did, int eid) {
        int count = 0;
        System.out.println("部门列表如下：");
        List<Dept> depts = S.queryAllDept(did);
        for (Dept dept : depts) {
            System.out.println("序号:" + ++count + "  " + dept.getName());
        }
        System.out.println("输入部门号查询该部门下员工简略信息，输入q或Q退出系统");
        String choice = null;
        choice = input.next();
        if (did != 2) {
            switch (choice) {
                case "1":
                    queryEmpByDept(did, did, 0, eid);
                    break;
                case "q":
                    logout();
                    break;
                case "Q":
                    logout();
                    break;
                default:
                    break;
            }
        } else {
            switch (choice) {
                case "1":
                    queryEmpByDept(2, 1, 0, eid);
                    break;

                case "2":
                    queryEmpByDept(2, 2, 0, eid);
                    break;
                case "3":
                    queryEmpByDept(2, 3, 0, eid);
                    break;
                case "4":
                    queryEmpByDept(2, 4, 0, eid);
                    break;
                case "5":
                    queryEmpByDept(2, 5, 0, eid);
                    break;
                case "6":
                    queryEmpByDept(2, 6, 0, eid);
                    break;
                case "7":
                    queryEmpByDept(2, 7, 0, eid);
                    break;
                case "8":
                    queryEmpByDept(2, 8, 0, eid);
                    break;
                case "q":
                    logout();
                    break;
                case "Q":
                    logout();
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 查部门员工
     *
     * @param did
     */
    public void queryEmpByDept(int id, int did, int from, int eid) {
        boolean status = true;
        int page = from;
        int i = S.selelctEmpCountByDept(did);
        int a = (int) Math.ceil(i / 5.0);
        while (status) {
            String str = " ";
            if (page >= a) {
                page = a - 1;
                str = "这是最后一页啦！！";
            } else if (page < 0) {
                page = 0;
                str = "这是第一页！！";
            }
            int count = 1;
            List<EmpVo> empVos2 = S.selelctEmpByDept(did, page * 5, eid);
            for (EmpVo empVo2 : empVos2) {
                System.out.println("序号：" + count++ + "\t" + empVo2.getLastname() + "\t" +
                        (empVo2.getGender() == 1 ? "男" : "女") + "\t" + empVo2.getMobile() + "\t" + empVo2.getMail());
            }
            for (int j = 1; j <= a; j++) {
                if (page + 1 == j) {
                    System.out.print("\t" + "[" + j + "]" + "\t");
                } else {
                    System.out.print("\t" + j + "\t");
                }
            }
            System.out.println();
            System.out.println(str);
            if (page == 0) {
                System.out.println("下一页请输入字母> n < " + "  " + "共" + a + "页" + "  " + "总记录数>> " + i + " 条");
            } else if (page == a - 1) {
                System.out.println("上一页请输入字母> p < " + "  " + "共" + a + "页" + "  " + "总记录数>> " + i + " 条");
            } else {
                System.out.println(" 上一页请输入字母> p < , 下一页请输入字母> n < ," + "共" + a + "页" + "  " + "总记录数>> " + i + " 条");
            }
            System.out.println("输入r返回部门列表，输入q或Q退出系统，请翻页>>>");
            String next = input.next();
            switch (next) {
                case "n":
                    ++page;
                    break;
                case "p":
                    --page;
                    break;
                case "r":
                    if (id == 2) {
                        status = false;
                        in01(2, eid);
                    } else {
                        status = false;
                        in01(did, eid);
                    }

                    break;
                case "Q":
                    status = false;
                    logout();
                    break;
                case "q":
                    status = false;
                    logout();
                    break;
                default:
                    System.out.println("请按 p 或 n 进行翻页，输入q或Q退出系统");
                    break;
            }
        }
    }
}
